package com.yjx.controller;


import com.github.pagehelper.PageInfo;
import com.yjx.service.OwnerService;
import com.yjx.service.RepairService;
import com.yjx.service.RepairtypeService;
import com.yjx.util.JsonObject;
import com.yjx.util.R;
import com.yjx.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 报修控制层
 */
@RestController
@Slf4j
@RequestMapping("repair")
public class RepairController {
    @Resource
    private RepairService repairService;
    @Resource
    private RepairtypeService repairtypeService;
    @Resource
    private OwnerService ownerService;

    /**
     * 管理员展示报修表格
     * @param repair
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("queryRepairAll")
    public JsonObject queryRepairAll(Repair repair, @RequestParam(defaultValue = "1" ) Integer page, @RequestParam(defaultValue = "15") Integer limit){
        PageInfo<Repair> pageInfo=repairService.findRepairAll(page,limit,repair);
        return new JsonObject(0,"ok",pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 业主展示报修表格
     * @param repair
     * @param request
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("queryRepairAll2")
    public JsonObject queryRepairAll2(Repair repair, HttpServletRequest request,
                                      @RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "15") Integer limit){
        //获取当前用户
        Userinfo userInfo=(Userinfo)request.getSession().getAttribute("user");
        String username=userInfo.getUsername();
        //根据用户名获得用户ID
        Owner owner=ownerService.queryOwnerByName(username);
        repair.setOwnerId(owner.getId());
        PageInfo<Repair> pageInfo=repairService.findRepairAll(page,limit,repair);
        return new JsonObject(0,"ok",pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 查询所有报修项目列表
     * @return
     */
    @RequestMapping("queryAll")
    public List<Repairtype> queryAll(){
        return repairtypeService.findList();
    }

    /**
     * 添加报修项目
     * @param repair
     * @param request
     * @return
     */
    @RequestMapping ("add")

    public R add(@RequestBody Repair repair,HttpServletRequest request){
        Userinfo userinfo=(Userinfo) request.getSession().getAttribute("user");
        String username=userinfo.getUsername();
        Owner owner=ownerService.queryOwnerByName(username);
        repair.setOwnerId(owner.getId());
        repair.setStatus(0);
        repair.setComDate(new Date());
        int num=repairService.add(repair);
        if(num>0){
            return R.ok();
        }
        return R.fail("新增失败!");

    }

    /**
     * 更新报修状态
     * @param id
     * @return
     */
    @RequestMapping ("update")
    public R update(Integer id,HttpServletRequest request){
        Userinfo userinfo=(Userinfo) request.getSession().getAttribute("user");
        String username=userinfo.getUsername();
        Repair repair=new Repair();
        repair.setId(id);
        repair.setStatus(1);
        repair.setHandleDate(new Date());
        repair.setClr(username);
        int num=repairService.updateData(repair);
        if(num>0){
            return R.ok();
        }
        return R.fail("更新失败!");
    }

    /**
     * 批量删除报修项目
     * @param ids
     * @return
     */
    @RequestMapping("deleteByIds")
    public R deleteByIds(String ids){
        List<String> list= Arrays.asList(ids.split(","));
        for(String id:list){
            repairService.delete(Long.parseLong(id));
        }
        return R.ok();

    }

    /**
     * 查询维修统计分析报表
     * @return
     */
    @RequestMapping("queryTongji")
    public List<Tongji> queryTongji(){
        return repairService.queryTongji();
    }





}
